define([
    'text!home/templates/HomeView.html',
    'i18n!home/i18n/HomeView.i18n'
], function(HomeViewTpl, i18nHomeView) {
    var alertHeight = 206;
    var timeOffset = -1;

    var myChart = {};
    var Chart = {};
    var toclean;
    return fish.View.extend({
        template: fish.compile(HomeViewTpl),
        i18nData: fish.extend({}, i18nHomeView),
        events: {},
        views: {
          // '.header': Portal.headerView = new HeaderView()
          // '.my-attention': [new ChartsView(), new ToolsView()]
        },

        //这里用来进行dom操作
//        _render: function() {
//            that = this;
//            this.$el.html(this.template(this.i18nData));
//            return this;
//        },

        //这里用来初始化页面上要用到的fish组件
        _afterRender: function() {
            console.info(this.i18nData);
            var me = this;
            $('#show,#spaceSize').height(($(window).height())-alertHeight);
            me.moduleSelect();
            me.warningInfo();
            // 点击事件
            me.sysmonitoringClick();
            me.approvalClick();
            me.moduleSelectedChange();
            me.timesSelectedClick();
            // 一些界面设置
            $('.timeSelect li').mouseover(function() {
                $(this).addClass('cur');
            });
            $('.timeSelect li').mouseout(function() {
                $(this).removeClass('cur');
            });
            $('.timeSelect li').click(function() {
                $('.timeSelect li').not($(this)).removeClass('active');
                $(this).addClass('active');
            });
            $(window).resize(function() {
                $('#show,#spaceSize').height(($(window).height())-alertHeight);
                myChart.resize();
                Chart.resize();
            }); 
        },

        //加载告警信息
        warningInfo: function(){
            var me = this;
            fish.ajax({
                url: webRoot + 'index/queryWarnningInfo.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: true, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseJSON;
                    if(result.length>0){
                        $('.Alert').show();
                        alertHeight = 238;
                    }
                    $('#show,#spaceSize').height(($(window).height())-alertHeight);
                    myChart.resize;
                    Chart.resize;
                    var text = "";
                    for(var i=0;i<result.length;i++){
                        text=text+result[i].warningDesc+";";
                    }
                    $("#warnningInfo").text(text); 
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        //加载业务系统下拉框选项 
        moduleSelect: function(){
            var me = this;
            fish.ajax({
                url: webRoot + 'index/searchSysmodule.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: true, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseJSON;
                    var dataset = eval(result);
                    var rsOption = '';
                    for(var i=0;i<dataset.length;i++){
                        rsOption +="<option value='"+dataset[i].moduleId+"'>"+dataset[i].moduleName+"</option>"; 
                    }
                    $("#module").append(rsOption); 
                    // 体检结果和空间使用情况
                    me.checkResult();
                    // 右上角任务信息
                    me.runTask();
                    me.failTask();
                    me.toCheckTask();
                    // 下面的任务信息
                    me.closedTask(-1);
                    me.errorTask(-1);
                    me.checkTask(-1);
                    me.backUpTask(-1);
                    me.cleanUpTask(-1); 
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        //加载体检结果
        checkResult: function(){var me = this;
            var nodeId = $("#module  option:selected").val();
            var sourceType = $('input:radio:checked').val();
            if (myChart && myChart.dispose) {
                myChart.dispose();
            }
            fish.ajax({
                url: webRoot + 'index/queryList4Circular.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: true, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseJSON;
                    myChart = echarts.init(document.getElementById('show'));
                    var option = {
                        title : {
                            text: '',
                            x:'center',
                            y:'center',
                            textStyle : {
                                    fontFamily : '微软雅黑',
                                    fontSize : 18,
                                    fontWeight : '100'
                                }
                        },
                        tooltip : {
                            trigger: 'item',
                            formatter: "{a} <br/>{b}"
                        },
                        legend: {
                            x : 'right',
                            data:[]
                        },
                        calculable : false,
                        series : [
                            {
                                name:'',
                                type:'pie',
                                radius : ['50%', '70%'],
                                data:[]
                            }
                        ],
                        noDataLoadingOption :{
                            text: '暂无体检、清理数据',
                            effect:'whirling',
                            textStyle: {
                                fontFamily : '微软雅黑',
                                fontSize: 26
                            }
                        }
                    }; 
                    if(result.success){                        
                        option.series[0].data = result.seriesData;
                        option.legend.data = result.legendData;
                        option.title.text = result.checkData;
                        $("#checkname").text("体检结果："); 
                        toclean = result.seriesData[0].value;
                    }else{
                        option.series[0].data =[];
                        option.legend.data = [];
                        option.title.text = "";
                        $("#checkname").text(""); 
                        toclean = 0;
                    }
                    setTimeout(function(){
                        myChart.setOption(option);
                        me.spaceResult();
                    },500);
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        //加载空间使用情况
        spaceResult: function(){
            var nodeId = $("#module  option:selected").val();
            var sourceType = $('input:radio:checked').val();
            if (Chart && Chart.dispose) {
                Chart.dispose();
            }
            fish.ajax({
                url: webRoot + 'index/queryList4Pie.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: true, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId, toClean: toclean},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseJSON;
                    Chart = echarts.init(document.getElementById('spaceSize'));
                    var optionSpace = {
                        color:['#ff6666','#37afff'],
                        title : {
                            text: '空间使用情况：',
                            x:'left',
                            y:'10',
                            textStyle : {
                                    fontFamily : '微软雅黑',
                                    fontSize : 18,
                                    fontWeight : '100'
                                }
                        },
                        tooltip : {
                            trigger: 'item',
                            formatter: "{b}({d}%)"
                        },
                        legend: {
                            x : 'right',
                            data:[]
                        },
                        calculable : false,
                        series : [
                            {
                                name:'',
                                type:'pie',
                                radius : '82%',
                                center: ['45%', '50%'],
                                data:[]
                            }
                        ],
                        noDataLoadingOption :{
                            text: '暂无空间使用情况',
                            effect:'whirling',
                            textStyle: {
                                fontFamily : '微软雅黑',
                                fontSize: 26
                            }
                        }
                    };
                    if(result.success){
                        optionSpace.series[0].data = result.seriesData;
                        optionSpace.legend.data = result.legendData;
                    }else{
                        optionSpace.series[0].data =[];
                        optionSpace.legend.data = [];
                    }
                    Chart.setOption(optionSpace);
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        //加载执行中的任务数
        runTask: function(){ 
            var nodeId = $("#module  option:selected").val();
            var sourceType = $('input:radio:checked').val();
            fish.ajax({
                url: webRoot + 'index/searchTaskNum.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: true, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId, state: "00R", offset: -1},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseJSON;
                    $("#runTask").text(result);
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        //加载失败的任务数
        failTask: function(){ 
            var nodeId = $("#module  option:selected").val();
            var sourceType = $('input:radio:checked').val();
            fish.ajax({
                url: webRoot + 'index/searchTaskNum.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: true, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId, state: "00F", isCheck: -1, offset: -1},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseJSON;
                    $("#failTask").text(result);
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        //加载待审核的任务数
        toCheckTask: function(){
            var nodeId = $("#module  option:selected").val();
            var sourceType = $('input:radio:checked').val();
            fish.ajax({
                url: webRoot + 'index/getIsCheckNum.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: true, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseJSON;
                    $("#toCheckTask").text(result);
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        //加载已执行的任务数
        closedTask: function(offset){ 
            var nodeId = $("#module option:selected").val();
            var sourceType = $('input:radio:checked').val();
            fish.ajax({
                url: webRoot + 'index/searchTaskNum.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: true, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId, state: "00A", isCheck: -1, offset: offset},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseJSON;
                    $("#closedTask").text(result);
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        //加载失败的任务数
        errorTask: function(offset){ 
            var nodeId = $("#module option:selected").val();
            var sourceType = $('input:radio:checked').val();
            fish.ajax({
                url: webRoot + 'index/searchTaskNum.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: true, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId, state: "00F", isCheck: -1, offset: offset},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseJSON;
                    $("#errorTask").text(result);
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            }); 
        },

        //加载体检的数据量
        checkTask: function(offset){ 
            var nodeId = $("#module option:selected").val();
            var sourceType = $('input:radio:checked').val();
            fish.ajax({
                url: webRoot + 'index/getCheckDataSize.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: false, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId, state: "00F", isCheck: -1, offset: offset},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseText;
                    $("#checkTask").text(result.substr(0,result.length-1));
                    if(result.indexOf('M') >= 0){
                        $("#checkTask_MG").text('M');
                    }else{
                        $("#checkTask_MG").text('G');
                    }
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(XHR,status,error){
                }
            });
        },

        //加载备份的数据量
        backUpTask: function(offset){ 
            var nodeId = $("#module option:selected").val();
            var sourceType = $('input:radio:checked').val();
            fish.ajax({
                url: webRoot + 'index/getBackUpDataSize.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: false, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId, offset: offset},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseText;
                    $("#backUpTask").text(result.substr(0,result.length-1));
                    if(result.indexOf('M') >= 0){
                        $("#backUpTask_MG").text('M');
                    }else{
                        $("#backUpTask_MG").text('G');
                    }
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        //加载清理的数据量
        cleanUpTask: function(offset){ 
            var nodeId = $("#module option:selected").val();
            var sourceType = $('input:radio:checked').val();
            fish.ajax({
                url: webRoot + 'index/getResultDataSize.do',
                showMask: false,  //是否自动显式全局遮罩
                showError: false, //在出现异常时自动显示异常信息，后台需要返回的数据格式：{code:'ERROR-1001',message:'登录失败'}
                timeout: 5000,   //超过3秒显式全局遮罩，此参数在showMask为true时才有效
                data: {sourceType: sourceType, nodeId: nodeId, offset: offset},
                beforeSend: function(){
                },
                complete: function(xhr, status){
                    var result = xhr.responseText;
                    $("#cleanUpTask").text(result.substr(0,result.length-1));
                    if(result.indexOf('M') >= 0){
                        $("#cleanUpTask_MG").text('M');
                    }else{
                        $("#cleanUpTask_MG").text('G');
                    }
                },
                //【注意】如果你在这里配置了自己的error处理函数，fish.ajax就不会自动帮你显示异常信息了！
                error:function(){
                }
            });
        },

        // 跳转到系统监控界面的点击事件
        sysmonitoringClick: function(){
            var me = this;
            $('#runTask').parent().click(function(){
                me.sysmonitoring('00R','');
            });
            $('#failTask').parent().click(function(){
                me.sysmonitoring('00F','');
            });
            $('#backUpTask').parent().parent().click(function(){
                me.sysmonitoring('',1);
            });
            $('#cleanUpTask').parent().parent().click(function(){
                me.sysmonitoring('',2);
            });
            $('#checkTask').parent().parent().click(function(){
                me.sysmonitoring('',0);
            });
            $('#closedTask').parent().parent().click(function(){
                me.sysmonitoring('','');
            });
            $('#errorTask').parent().parent().click(function(){
                me.sysmonitoring('00F','');
            });
        },

        //跳转到系统监控界面
        sysmonitoring: function(state,stage){
            var iocpUrl = document.referrer;
            iocpUrl = iocpUrl.substring(0,iocpUrl.indexOf('portal'));
            $.ajax({
                url : iocpUrl+'base/sendMessage.do?message=openUrlqq("'+webRoot+sysmonitoringURL+'?state='+state+'%26stage='+stage+'%26time='+timeOffset+'","'+sysmonitoringName+'",'+sysmonitoringId+')&channel='+parentMid,
                dataType : "jsonp",
                timeout : 3000,
                jsonpCallback:"jsonData",
                success:function(data){
                },
                error:function(){
                }
            });
        },

        // 跳转到数据审批界面的点击事件
        approvalClick: function(){
            var me = this;
            $('#toCheckTask').parent().click(function(){
                me.approval();
            });
        },

        //跳转到数据审批界面
        approval: function(){
            var iocpUrl = document.referrer;
            iocpUrl = iocpUrl.substring(0,iocpUrl.indexOf('portal'));
            $.ajax({
                url : iocpUrl+'base/sendMessage.do?message=openUrlqq("'+webRoot+approvalURL+'","'+approvalName+'",'+approvalId+')&channel='+parentMid,
                dataType : "jsonp",
                timeout : 3000,
                jsonpCallback:"jsonData",
                success:function(data){
                },
                error:function(){
                }
            });
        },

        // 切换业务系统的change事件
        moduleSelectedChange: function(){
            var me = this;
            $('#module').change(function(){
                me.moduleSelected();
            });
            $('#table').change(function(){
                me.moduleSelected();
            });
            $('#file').change(function(){
                me.moduleSelected();
            });
        },

        //切换业务系统
        moduleSelected: function(){
            var me = this;
            me.runTask();
            me.failTask();
            me.toCheckTask();
            me.closedTask(-1);
            me.errorTask(-1);
            me.checkTask(-1);
            me.backUpTask(-1);
            me.cleanUpTask(-1);
            me.checkResult();
        },

        // 切换时段点击事件
        timesSelectedClick: function(){
            var me = this;
            $('.timeSelect>li').click(function(){
                var timeText = $(this).text().trim();
                if(timeText == me.i18nData.No_Period_Time){
                    me.timesSelected(-1);
                }else if(timeText == me.i18nData.Nearly_One_Month){
                    me.timesSelected(1);
                }else if(timeText == me.i18nData.Nearly_Three_Month){
                    me.timesSelected(3);
                }else if(timeText == me.i18nData.Nearly_Six_Month){
                    me.timesSelected(6);
                }else if(timeText == me.i18nData.Nearly_Twelve_Month){
                    me.timesSelected(12);
                }
            });
        },

        //切换时间段
        timesSelected: function(offset){
            var me = this;
            timeOffset = offset;
            me.closedTask(offset);
            me.errorTask(offset);
            me.checkTask(offset);
            me.backUpTask(offset);
            me.cleanUpTask(offset);
        }
    });
});